home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / HrefButtonArea.java < prev    next >
Text File  |  1998-09-15  |  5KB  |  148 lines

  1. /*
  2.  * @(#)HrefButtonArea.java    1.8 98/03/18
  3.  *
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.Graphics;
  32. import java.awt.Image;
  33. import java.net.URL;
  34. import java.net.MalformedURLException;
  35.  
  36. /**
  37.  * An improved "Fetch a URL" ImageArea class.
  38.  * This class extends the basic ImageArea Class to fetch a URL when
  39.  * the user clicks in the area.  In addition, special custom highlights
  40.  * are used to make the area look and feel like a 3-D button.
  41.  *
  42.  * @author     Jim Graham
  43.  * @version     1.8, 03/18/98
  44.  */
  45. class HrefButtonArea extends ImageMapArea {
  46.     /** The URL to be fetched when the user clicks on this area. */
  47.     URL anchor;
  48.     /** The highlight image for when the button is "UP". */
  49.     Image upImage;
  50.     /** The highlight image for when the button is "DOWN". */
  51.     Image downImage;
  52.     /** This flag indicates if the "button" is currently pressed. */
  53.     boolean pressed = false;
  54.     /** The border size for the 3-D effect. */
  55.     int border = 5;
  56.  
  57.     /**
  58.      * The argument string is the URL to be fetched.
  59.      * This method also constructs the various highlight images needed
  60.      * to achieve the 3-D effect.
  61.      */
  62.     public void handleArg(String arg) {
  63.     try {
  64.         anchor = new URL(parent.getDocumentBase(), arg);
  65.     } catch (MalformedURLException e) {
  66.         anchor = null;
  67.     }
  68.     if (border * 2 > W || border * 2 > H) {
  69.         border = Math.min(W, H) / 2;
  70.     }
  71.     }
  72.  
  73.     public void makeImages() {
  74.     upImage = parent.getHighlight(X, Y, W, H,
  75.                       new ButtonFilter(false,
  76.                                parent.hlpercent,
  77.                                border, W, H));
  78.     downImage = parent.getHighlight(X, Y, W, H,
  79.                     new ButtonFilter(true,
  80.                              parent.hlpercent,
  81.                              border, W, H));
  82.     }
  83.  
  84.     public boolean imageUpdate(Image img, int infoflags,
  85.                    int x, int y, int width, int height) {
  86.     if (img == (pressed ? downImage : upImage)) {
  87.         return parent.imageUpdate(img, infoflags, x + X, y + Y,
  88.                       width, height);
  89.     } else {
  90.         return (img == downImage || img == upImage);
  91.     }
  92.     }
  93.  
  94.     /**
  95.      * The isTerminal method indicates whether events should propagate
  96.      * to the areas underlying this one.
  97.      */
  98.     public boolean isTerminal() {
  99.     return true;
  100.     }
  101.  
  102.     /**
  103.      * The status message area is updated to show the destination URL.
  104.      * The graphical highlight is achieved using the ButtonFilter.
  105.      */
  106.     public void highlight(Graphics g) {
  107.     if (entered) {
  108.         g.drawImage(pressed ? downImage : upImage, X, Y, this);
  109.     }
  110.     }
  111.  
  112.     public void enter() {
  113.     showStatus((anchor != null)
  114.            ? "Go To " + anchor.toExternalForm()
  115.            : null);
  116.     repaint();
  117.     }
  118.  
  119.     public void exit() {
  120.     showStatus(null);
  121.     repaint();
  122.     }
  123.  
  124.     /**
  125.      * Since the highlight changes when the button is pressed, we need
  126.      * to record the "pressed" state and induce a repaint.
  127.      */
  128.     public boolean press() {
  129.     pressed = true;
  130.     repaint();
  131.     return true;
  132.     }
  133.  
  134.     /**
  135.      * The new URL is fetched when the user releases the mouse button
  136.      * only if they are still in the area.
  137.      */
  138.     public boolean lift(int x, int y) {
  139.     pressed = false;
  140.     repaint();
  141.     if (inside(x, y) && anchor != null) {
  142.         showDocument(anchor);
  143.     }
  144.     return true;
  145.     }
  146. }
  147.  
  148.